package application.common.cron4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.cron4j.ITask;
import application.common.model.Store;
import application.common.render.HttpClientResult;
import application.common.util.HttpUtil;
import application.common.util.ShiroUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class StoreTask implements ITask{
	
	private boolean isEnd=true;
	public void run() {
		if(isEnd && ShiroUtil.getConfig()!=null && StrKit.equals(ShiroUtil.getConfig().getState(),"0")) {
			isEnd=false;
			if(ShiroUtil.getConfig()!=null && !StrKit.isBlank(ShiroUtil.getConfig().getSynURL())) {
				try {
					List<Store> stores=Store.dao.findAll();
					if(stores!=null && stores.size()>0) {
						Map<String, String> params =new HashMap<String,String>();
						params.put("rows", JsonKit.toJson(stores));
						params.put("cabinetId", ShiroUtil.getAuth());
						HttpClientResult httpClientResult=HttpUtil.doPost(String.format("%s/api/cab/store/saveStores", ShiroUtil.getConfig().getSynURL()), params);
						if(httpClientResult==null) {
							isEnd=true;
							return;
						}
						if(httpClientResult.getCode()!=200) {
							isEnd=true;
							return;	
						}
						String content=httpClientResult.getContent();
						if(StrKit.isBlank(content)) {
							isEnd=true;
							return;	
						}
						JSONObject jsonObject=JSONObject.fromObject(content);
						if(jsonObject==null) {
							isEnd=true;
							return;	
						}
						if(jsonObject.has("code")) {
							int code=jsonObject.getInt("code");
							if(code!=200) {
								isEnd=true;
								return;	
							}
							
							if(!jsonObject.has("data")) {
								isEnd=true;
								return;
							}
							String data=jsonObject.getString("data");
							if(StrKit.isBlank(data)) {
								isEnd=true;
								return;	
							}
							JSONArray array=JSONArray.fromObject(data);
							if(array==null || array.size()==0) {
								isEnd=true;
								return;
							}
							for(Object object:array) {
								JSONObject row=JSONObject.fromObject(object);
								if(row!=null) {
									if(row.containsKey("id")) {
										Store store=Store.dao.findById(row.getString("id"));
										if(store!=null) {
											if(row.containsKey("field1") && !StrKit.equals(row.getString("field1"), store.getField1())) {
												store.setField1(row.getString("field1"));
											}
											if(row.containsKey("field2")&& !StrKit.equals(row.getString("field2"), store.getField2())) {
												store.setField2(row.getString("field2"));
											}
											if(row.containsKey("field3")&& !StrKit.equals(row.getString("field3"), store.getField3())) {
												store.setField3(row.getString("field3"));
											}
											if(row.containsKey("field4")&& !StrKit.equals(row.getString("field4"), store.getField4())) {
												store.setField4(row.getString("field4"));
											}
											if(row.containsKey("field5")&& !StrKit.equals(row.getString("field5"), store.getField5())) {
												store.setField5(row.getString("field5"));
											}
											if(row.containsKey("status")&& !StrKit.equals(row.getString("status"), store.getStatus())) {
												store.setStatus(row.getString("status"));
											}
											if(ShiroUtil.getConfig()!=null && StrKit.equals(ShiroUtil.getConfig().getState(),"0")) {
												store.update();
											}
										}
									}
								}
							}
							isEnd=true;
						}else {
							isEnd=true;
						}
					}else {
						isEnd=true;
					}
				}catch (Exception e) {
					isEnd=true;
				}
			}
			isEnd=true;
		}
	}

	public void stop() {
	}

}
